﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

// ReSharper disable StaticMemberInGenericType
// ReSharper disable CollectionNeverUpdated.Global
// ReSharper disable CollectionNeverUpdated.Local

namespace EntityFrameworkHelper.Migrations
{
    /// <summary>
    ///     Class MigrationsConfiguration.
    /// </summary>
    /// <typeparam name="TContext">The type of the t context.</typeparam>
    public class MigrationsConfiguration<TContext> : DbMigrationsConfiguration<TContext>
        where TContext : DbContext
    {
        /// <summary>
        ///     获取 数据迁移初始化种子数据操作信息集合，各个模块可以添加自己的数据初始化操作
        /// </summary>
        /// <value>The seed actions.</value>
        private static readonly ICollection<ISeedAction> SeedActions;

        /// <summary>
        ///     Initializes static members of the <see cref="MigrationsConfiguration{TContext}" /> class.
        /// </summary>
        static MigrationsConfiguration()
        {
            SeedActions = new List<ISeedAction>();
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="MigrationsConfiguration{TContext}" /> class.
        /// </summary>
        public MigrationsConfiguration()
        {
            this.AutomaticMigrationsEnabled = true;
            this.AutomaticMigrationDataLossAllowed = true;
        }

        /// <summary>
        ///     Seeds the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void Seed(TContext context)
        {
            IEnumerable<ISeedAction> seedActions = SeedActions.OrderBy(m => m.Order);
            foreach (ISeedAction seedAction in seedActions)
            {
                seedAction.Action(context);
            }
        }
    }
}